myApp
    .controller('customerTemplateCtrl', function ($cookieStore,$scope, $http,CONFIG ,$window) {

        /*根据user cookie 初始化加载菜单*/
        $scope.userName = $cookieStore.get("userName");

        /*初始化界面，加载所有会员*/
        var url = CONFIG.mainConfig.url+"/customer/queryAllCustomer";
        $http({
            method: 'post',
            url: url,
            params: {
                token: $cookieStore.get("token")
            }
        })
            .success(function (result) {

                if (result.resultCode == 001) {
                    $scope.customers = result.customerList;
                }
                else {
                    alert(result.resultMsg);
                }

            })
            .error(function () {
                alert("error");
            });

        /*添加模板*/
        $scope.addMenuTemplate = function () {
            $window.location.href = "/html/addCustomer.html";
        }

        /*保存编辑后的模板*/
        $scope.saveCustomer = function (num, phone, name, sex,age,preNum) {

            /*保存到DB*/
            var url = CONFIG.mainConfig.url+"/customer/addCustomer";

            if(sex=="男"){
                sex=1;
            }else if(sex=="女"){
                sex=2;
            }

            if(phone!=null&&phone.length!=11){
                alert("请输入正确的手机号码");
                return;
            }

            $http({
                method: 'post',
                url: url,
                params: {
                    num: num,
                    phone: phone,
                    name: name,
                    sex: sex,
                    age:age,
                    preNum:preNum,
                    token: $cookieStore.get("token")
                }
            })
                .success(function (result) {

                    if (result.resultCode == 001) {
                        //TODO:
                        $window.location.href = "/html/customer.html";
                    }
                    else {
                        alert(result.resultMsg);
                    }

                })
                .error(function () {
                    alert("error");
                });
        }

        /*删除一个菜单模板*/
        $scope.deleteTemplate = function (num, index) {
            /*从界面删除*/
            $scope.customers.splice(index, 1);
            /*从db删除*/
            var url = CONFIG.mainConfig.url+"/customer/deleteCustomerByNum";
            $http({
                method: 'post',
                url: url,
                params: {
                    num: num,
                    token: $cookieStore.get("token")
                }
            })
                .success(function (result) {

                    if (result.resultCode == 001) {
                        //TODO:处理成功
                    }
                    else {
                        alert(result.resultMsg);
                    }

                })
                .error(function () {
                    alert("error");
                });
        }
    });